package com.yssw.account.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.yssw.account.domain.AccountHead;
import com.yssw.account.domain.AccountTypeEnums;
import com.yssw.account.mapper.AccountHeadMapper;
import com.yssw.account.service.CalculationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yssw.account.mapper.AccountTableMapper;
import com.yssw.account.domain.AccountTable;
import com.yssw.account.service.IAccountTableService;

/**
 * 账户信息Service业务层处理
 *
 * @author yssw
 * @date 2023-04-04
 */
@Service
public class AccountTableServiceImpl implements IAccountTableService {
    @Autowired
    private AccountTableMapper accountTableMapper;
    @Autowired
    private AccountHeadMapper accountHeadMapper;



    /**
     * 查询账户信息
     *
     * @param id 账户信息主键
     * @return 账户信息
     */
    @Override
    public AccountTable selectAccountTableById(Long id) {
        AccountTable accountTable = accountTableMapper.selectAccountTableById(id);
        AccountHead accountHead = new AccountHead();
        accountHead.setAccountId(id);
        accountHead.setStatus("1");
        AtomicReference<BigDecimal> amount= new AtomicReference<>(new BigDecimal(accountTable.getInitialAmount() + ""));
        List<AccountHead> accountHeads = accountHeadMapper.selectAccountHeadList(accountHead);
        accountHeads.forEach(head->{
            CalculationService calculationService = updateBalance(head.getType());
            BigDecimal calculation = calculationService.calculation(amount.get(), head.getDiscountMoney());
            amount.set(calculation);
        });
        accountTable.setCurrentAmount(amount.get());
        return accountTable;
    }


    /**
     * 查询账户信息列表
     *
     * @param accountTable 账户信息
     * @return 账户信息
     */
    @Override
    public List<AccountTable> selectAccountTableList(AccountTable accountTable) {
        List<AccountTable> accountTables = accountTableMapper.selectAccountTableList(accountTable);
        accountTables.forEach(account->{
            AccountHead accountHead = new AccountHead();
            accountHead.setAccountId(account.getId());
            accountHead.setStatus("1");
            AtomicReference<BigDecimal> amount= new AtomicReference<>(new BigDecimal(account.getInitialAmount() + ""));
            List<AccountHead> accountHeads = accountHeadMapper.selectAccountHeadList(accountHead);

            accountHeads.forEach(head->{
                CalculationService calculationService = updateBalance(head.getType());
                BigDecimal calculation = calculationService.calculation(amount.get(), head.getDiscountMoney());
                amount.set(calculation);

            });
            account.setCurrentAmount(amount.get());
        });
        return accountTables;
    }

    /**
     * 新增账户信息
     *
     * @param accountTable 账户信息
     * @return 结果
     */
    @Override
    public int insertAccountTable(AccountTable accountTable) {
        return accountTableMapper.insertAccountTable(accountTable);
    }

    /**
     * 修改账户信息
     *
     * @param accountTable 账户信息
     * @return 结果
     */
    @Override
    public int updateAccountTable(AccountTable accountTable) {
        return accountTableMapper.updateAccountTable(accountTable);
    }

    /**
     * 批量删除账户信息
     *
     * @param ids 需要删除的账户信息主键
     * @return 结果
     */
    @Override
    public int deleteAccountTableByIds(Long[] ids) {
        return accountTableMapper.deleteAccountTableByIds(ids);
    }

    /**
     * 删除账户信息信息
     *
     * @param id 账户信息主键
     * @return 结果
     */
    @Override
    public int deleteAccountTableById(Long id) {
        return accountTableMapper.deleteAccountTableById(id);
    }

    @Override
    public CalculationService updateBalance(int type) {

        CalculationService calculation = AccountTypeEnums.getCalculation(type);
        return calculation;
    }


}
